﻿using Chloe;
using lingopen.Application.System;
using lingopen.Entity;
using lingopen.Entity.BLE;
using System.Collections.Generic;

namespace lingopen.Application.BLE.Implements
{
    public class BLEEmpAppService : AdminAppService, IBLEEmpAppService
    {
        /// <summary>
        /// 查询员工类别
        /// </summary>
        /// <param name="pagination">分页</param>
        /// <param name="keyword">关键字</param>
        /// <returns></returns>
        public PagedData<BLE_Emp_Type> QueryEmpTypes(Pagination pagination, string keyword)
        {
            var q = this.DbContext.Query<BLE_Emp_Type>().FilterDeleted().WhereIfNotNullOrEmpty(keyword, a => a.Name.Contains(keyword) || a.Code.Contains(keyword));
            q = q.OrderBy(a => a.Code);
            PagedData<BLE_Emp_Type> ret = q.TakePageData(pagination);
            return ret;
        }

        public List<SimpleModel> GetSimpleEmpTypeModels()
        {
            var q = this.DbContext.Query<BLE_Emp_Type>().FilterDeletedAndDisabled();
            q = q.OrderBy(a => a.Code);

            return q.Select(a => new SimpleModel() { Id = a.Id, Name = a.Name }).ToList();
        }


        /// <summary>
        /// 查询员工发卡信息
        /// </summary>
        /// <param name="pagination">分页</param>
        /// <param name="keyword">关键字</param>
        /// <returns></returns>
        public PagedData<BLE_Emp_Card_Model> QueryEmpCards(Pagination pagination, string keyword)
        {
            var q = this.DbContext.Query<BLE_Emp_Card>().FilterDeleted().WhereIfNotNullOrEmpty(keyword, a => a.Name.Contains(keyword) || a.Code.Contains(keyword) || a.MAC.Contains(keyword));
            q = q.OrderBy(a => a.Code);
            var lefjoin = q.LeftJoin<Entity.System.Sys_Config>((a, b) => b.Code == "BatteryLow");
            var query = lefjoin.Select((a, b)=> new BLE_Emp_Card_Model(a) { BatteryLow=int.Parse(b.Value)});
            PagedData<BLE_Emp_Card_Model> ret = query.TakePageData(pagination);
            return ret;
        }


    }
}
